<?php
//
// +--------------------------------------+
// |PHP version 5.5
// +--------------------------------------+
// |知果汇后台登录模块
// +--------------------------------------+
// |2017-12-26
// +--------------------------------------+
// |潘龙兵
// +--------------------------------------+
// |知果汇后台管理
// +--------------------------------------+
//
namespace app\api\controller;

use think\View;
use think\Controller;
use think\Db;
use \app\data\Data;

class Login extends Index
{
    public function test_login()
    {
        \think\Config::set("think_email.SMTP_HOST", "smtp.164.com");
        $a = \think\Config::get("think_email.SMTP_HOST");
        echo $a;
        exit;

        $username = input('get.username');
        // echo $username;exit;
        $password = input('get.password');
        $where['username|email|phone'] = $username;
        $where['status'] = 1;
        $zgh_user = new Data('user');
        $result = $zgh_user->_find($where);
        // dump($result);exit;
        // return jsonp($result);
        $id = $result['uid'];
        $username = $result['username'];
        session_start();
        $zgh_user_info = array(
            'id' => $id,
            'username' => $username
        );
        $zgh_user_info = json_encode($zgh_user_info);
        // dump($zgh_user_info);exit;
        session('zgh_user_info', $zgh_user_info);  //设置session
        cookie('zgh_user_info', $zgh_user_info, 3600 * 24 * 7);
        // echo $res;exit;
        // echo 123;exit;
        return jsonp('登录成功', '200');
    }

    public function test_index()
    {
        $data = model('Common')->_zgh_user_info();

        $json = json_encode($data);
        $callback = $_GET['callback'];
        exit($callback . "($json)");
        // echo json_encode($data);exit;
        // return jsonp($data,'200');//返回cookie值
        // $this -> json('200','登录成功',$data);
    }

    /*
    * @知果汇
    * @检验登录状态
    * @2017-12-26
    * @潘龙兵
    * @$_fmkx_user_info：保存的用户信息
    *  $username：用户名
    *  $token:  唯一登录标识
    *  $where['status']:  用户状态 0:未启用 1:启用 2:删除
    *  $sql_token:  数据库中存储的唯一登录标识
    */
    public function index()
    {
        $zgh_user_info = model('Common')->_zgh_admin_info();
	
        $uid = $zgh_user_info['id'];
        $where['uid'] = $uid;
        $where['status'] = 1;
        $where['role'] = 1;
        $result = Db::table('zgh_user')->where($where)->find();
        // dump($result);exit;
        if (isset($result)) {
            $this->json('200', '登录成功', null);
        } else {
            cookie('zgh_user_info', null);
            session('zgh_user_info', null);
            $this->json('1000', '登录失败', null);
        }
    }


    /*
    * @知果汇
    * @获取验证码
    * @2017-12-26
    * @潘龙兵
    * 
    */
    public function get_code()
    {
        $code = model('Common')->GetRandStr();
        session('code', $code);
        cookie('code', $code);
        $this->json('200', '获取验证码', $code);
    }


    /*
    * @知果汇
    * @后台登录操作
    * @2017-12-26
    * @潘龙兵
    * @$user：post得到的用户名
    *  $password：post得到的密码
    *  $verify: 验证码
    *  $rand: 随机数
    *  $token:  登录标识,存储SESSIONID
    *  $id:  用户id
    *  $username:  用户名
    *  $zgh_user_info: 存储登录信息，json格式
    */
    public function login()
    {
        $user = input('post.username');
        $password = input('post.password');
        $where['username|email|phone'] = $user;
        $where['status'] = 1;
        $zgh_user = new Data('user');
        $result = $zgh_user->_find($where);
        // dump($result);exit;
        if (!$result) $this->json('1000', "用户名错误", null); //用户不存在
//        if($result['role'] != 1) $this->json('1000','当前不是管理员身份',null);
        $rand = $result['rand'];
        $password = model('Common')->change_pwd($password, $rand);
        if ($password != $result['password']) $this->json('1000', '密码错误', null); //密码错误
        // dump($result);exit;
        session_start();
        $id = $result['uid'];
        $username = $result['username'];
        $zgh_user_info = array(
            'id' => $id,
            'username' => $username
        );
        $zgh_user_info = json_encode($zgh_user_info);
        // dump($zgh_user_info);exit;
        action_log('管理员登录', 'log_admin',1,$username);
        session('zgh_user_info', $zgh_user_info);  //设置session
        $this->json('200', '管理员登录成功', $result);
    }


}